//
//  UIView+Gradient.m
//  SeaWorld
//
//  Created by 专注 on 2017/11/21.
//  Copyright © 2017年 Rice. All rights reserved.
//

#import "UIColor+WLGradient.h"

@implementation UIColor (WLGradient)
/**
 生成渐变色
 */
+ (UIColor*)fromColor:(UIColor*)fromColor
                 toColor:(UIColor*)toColor
                  length:(int)length
            isHorizontal:(BOOL)isHorizontal{
    CGSize size;
    if (isHorizontal) {
        size = CGSizeMake(length, 1);
    }
    else{
        size = CGSizeMake(1, length);
    }
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    NSArray* colors = [NSArray arrayWithObjects:(id)fromColor.CGColor, (id)toColor.CGColor, nil];
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
    
    //定义起始点和终止点
    CGPoint start = CGPointMake(0, 0);
    CGPoint end;
    if (isHorizontal) {
        end = CGPointMake(length, 0);
    }
    else{
        end = CGPointMake(0, length);
    }
    CGContextDrawLinearGradient(context, gradient, start, end, 0);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();

    return [UIColor colorWithPatternImage:image];
}

/**
 生成渐变色
 */
+ (UIColor*)colorArray:(NSArray <UIColor*> *)colorArray
                  length:(int)length
            isHorizontal:(BOOL)isHorizontal{
    CGSize size;
    if (isHorizontal) {
        size = CGSizeMake(length, 1);
    }
    else{
        size = CGSizeMake(1, length);
    }
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    NSMutableArray* colors = [NSMutableArray array];
    for (UIColor *color in colorArray) {
        [colors addObject:(id)color.CGColor];
    }
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
    
    //定义起始点和终止点
    CGPoint start = CGPointMake(0, 0);
    CGPoint end;
    if (isHorizontal) {
        end = CGPointMake(length, 0);
    }
    else{
        end = CGPointMake(0, length);
    }
    CGContextDrawLinearGradient(context, gradient, start, end, 0);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();

    return [UIColor colorWithPatternImage:image];
}
+ (UIColor *)currentColoc:(UIColor *)currentColoc
                 targetColoc:(UIColor *)targetColoc
                  percentage:(CGFloat)pageDisTage{
    NSArray * selectComponents = [self getRGBWithColor:currentColoc];
    NSArray * nonselectComponents = [self getRGBWithColor:targetColoc];
    
    CGFloat  SR = [selectComponents[0] floatValue];
    CGFloat  SG = [selectComponents[1] floatValue];
    CGFloat  SB = [selectComponents[2] floatValue];
    
    CGFloat  NR = [nonselectComponents[0] floatValue];
    CGFloat  NG = [nonselectComponents[1] floatValue];
    CGFloat  NB = [nonselectComponents[2] floatValue];
    
    CGFloat  RX = (SR - NR) * pageDisTage;
    CGFloat  GX = (SG - NG) * pageDisTage;
    CGFloat  BX = (SB - NB) * pageDisTage;
    
    int rs = SR-RX;
    int gs = SG-GX;
    int bs = SB-BX;
    UIColor *nowColor = [UIColor colorWithRed:rs/255.f green:gs/255.f blue:bs/255.f alpha:1];
    return nowColor;
}

+ (NSArray *)getRGBWithColor:(UIColor *)color
{
    CGFloat red = 0.0;
    CGFloat green = 0.0;
    CGFloat blue = 0.0;
    CGFloat alpha = 0.0;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    return @[@(red*255.f), @(green*255.f), @(blue*255.f), @(alpha)];
}


// 改变UIColor的Alpha
- (UIColor *)initColorAlpha:(CGFloat)alpha{
    UIColor *col = [self copy];
    NSArray * components = [UIColor getRGBWithColor:col];
    CGFloat  SR = [components[0] floatValue];
    CGFloat  SG = [components[1] floatValue];
    CGFloat  SB = [components[2] floatValue];
    UIColor *newColor = [UIColor colorWithRed:SR/255.f green:SG/255.f blue:SB/255.f alpha:alpha];
    return newColor;
}

@end
